package edu.chen;

import com.sun.org.apache.xml.internal.resolver.Catalog;
import edu.chen.bean.Category;
import edu.chen.bean.Product;
import edu.chen.bean.User;
import net.sf.ehcache.statistics.beans.ProxiedDynamicMBean;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.util.HashSet;
import java.util.Set;

public class test {
    public static void main(String[] args) {

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        try {
            session.beginTransaction();

//            Category category = new Category();
//            category.setName("Java");
//
//            session.save(category);
//
//            Product product = new Product();
//            product.setName("Script");
//            product.setPrice(10.1);
//            product.setCategory(category);
//
//            session.save(product);

//            Category category = session.get(Category.class,1);
//            for (Product product : category.getProductSet()){
//                System.out.println(product);
//            }
//            session.getTransaction().commit();

//            Product product2 = session.get(Product.class,1);
//            Set<Product> products = new HashSet<Product>();
//            products.add(product2);
//
//            User user = session.get(User.class,2);
//            user.setProducts(products);
//
//            session.save(user);

            Product product = new Product();
            product.setName("SpringFrame");
            product.setPrice(100.0);
            product.setCategory(session.get(Category.class,1));
            session.save(product);

            User user = session.get(User.class,2);
            user.getProducts().add(product);
            session.update(user);

            session.getTransaction().commit();

        }finally {
            session.close();
            sessionFactory.close();
        }

    }
}
